package com.ghbank.rdws.batchservice.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ghbank.rdws.batchservice.dto.BatchDTO;
import com.ghbank.rdws.batchservice.entity.BatchLog;
import com.ghbank.rdws.batchservice.service.BatchService;
import com.ghbank.rdws.common.http.GhbankResponseEntity;
import com.ghbank.rdws.common.utils.BeanUtils;
import com.ghbank.rdws.common.utils.IPUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "批量处理控制器")
@RestController
@Slf4j
public class BatchController {
    @Autowired
    private BatchService batchService;
    
    @ApiOperation(value = "批量程序执行", notes = "根据token获取批量处理程序")
    @ApiImplicitParam(name = "dto",value = "批次号和回调地址",required = true,dataType = "BatchDTO")
    @PostMapping(value = "/rdws_batch/execute")
    public GhbankResponseEntity<Integer> execute(@Valid @RequestBody BatchDTO dto, HttpServletRequest request) {
        BatchLog entity = BeanUtils.transform(BatchLog.class, dto);
        String ipAddr = IPUtils.getIpAddr(request);
        entity.setIpAddr(ipAddr);
        try {
            int result = batchService.execute(entity);
            return GhbankResponseEntity.ok(result, dto.getSeq());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return GhbankResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), dto.getSeq());
        }
        
    }
}
